package com.bbz.core.lambda.java8.Chapter_7_parallel;

import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Spliterator是Java 8中加入的另一个新接口；这个名字代表“可分迭代器”（splitable
 * iterator）。和Iterator一样，Spliterator也用于遍历数据源中的元素，但它是为了并行执行
 * 而设计的
 */
public class Spliterator {
    static  final String SENTENCE =
            " Nel mezzo del cammin di nostra vita " +
                    "mi ritrovai in una selva oscura" +
                    " ché la dritta via era smarrita ";
    public static  void main(String args[]){
        System.out.println("Found " + countWordsIteratively(SENTENCE) + " words");
        Stream<Character> stream=IntStream.range(0,SENTENCE.length()).mapToObj(SENTENCE::charAt);
    }
    //传统方式查找单词个数
    public static  int countWordsIteratively(String s) {
        int counter = 0;
        boolean lastSpace = true;
        for (char c : s.toCharArray()) {
            if (Character.isWhitespace(c)) {
                lastSpace = true;
            } else {
                if (lastSpace) counter++;
                lastSpace = false;
            }
        }
        return counter;
    }
}
